#include "window.h"

Window::Window(QWidget *parent) :
    QWidget(parent)
{
    recoBase();

    QPalette p = QPalette();
    p.setColor(QPalette::Window,Qt::black);

    this->setPalette(p);

    b = new Bucquage(this);
    w = new Widget(this);

    QHBoxLayout *l = new QHBoxLayout(this);
    l->addWidget(w);
    l->addWidget(b);

    b->setVisible(false);

    connect(b,SIGNAL(termine()),this,SLOT(finirBucquage()));
    connect(w,SIGNAL(identifier(Pg*)),this,SLOT(commencerBucquage(Pg*)));



}

void Window::commencerBucquage(Pg *p)
{
    w->setVisible(false);
    b->setPg(p);
    b->setVisible(true);
}

void Window::finirBucquage()
{
    b->setVisible(false);
    b->resetModels();
    w->majTextEdits();
    w->setVisible(true);
    w->setFocus();
}

void Window::recoBase()
{
    if(ConnexionBDD::coGuest()<0)
    {
        QMessageBox::warning(this,"Erreur","Connexion impossible");
    }
}

void Window::closeEvent(QCloseEvent *e)
{

    //On demande le mot de passe pour fermer le programme
    QSettings s;
    bool ok = false;
    QString pass = QInputDialog::getText(this,"Mot de pass","Veuillez taper le mot de pass pour autoriser la fermeture.",QLineEdit::Password,"",&ok);

    if(QVariant(QCryptographicHash::hash(pass.toUtf8(),QCryptographicHash::Sha1)).toString() == s.value("pass").toString() && ok){
        w->fermer_base();
        e->accept();
    }else{
        e->ignore();
    }


}
